import kotlinx.coroutines.*
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test

class SimpleCacheTest {
    private val cache = SimpleCache()

    @Test
    fun setTest(){
        cache.set("1",1)
        Assertions.assertEquals(1, cache.get("1"))
    }

    @Test
    fun concurrentSetTest(){
        runBlocking {
            val job1 = async(Dispatchers.Default) { set(cache) }
            val job2 = async(Dispatchers.Default) { set(cache) }

            job1.await()
            job2.await()
        }

        Assertions.assertEquals(2_000_000, cache.size)
    }

    private suspend fun set(cache: Cache){
        (1..1_000_000).map {
            cache.set(it.toString(), it)
        }
        delay(1000L)
    }

}